<?php

namespace app\index\controller;

use app\common\controller\HomeBase;
use think\Db;

class Lists extends HomeBase {

    
    public function index() {

        $order_by = 'add_time desc';
        
        $id = input('param.id',71);
        $pid = input('param.pid');
        
        $sort = input('param.sort');
        if($sort){
            $sort_arr = explode('-', $sort);
            if(in_array($sort_arr[1],array('asc','desc')) && in_array($sort_arr[0], array('add_time','click'))){
                $order_by = $sort_arr[0].' '.$sort_arr[1];
            }
        }
        
        $data = getChildMovies($pid ? $pid : $id,$order_by,32);
        
        //获取日周月排行
        $ranking = getColumnRanking($pid ? $pid : $id,10);
        
        //获取电影类目
        $pl = getCategoryChildren(0);
        
        if(isset($pid) && $pid){
            //找到父亲
            $pid2 = Db::name('Category')->find($pid)['pid'];
            //获取类目下面的小类目
            $zl = getCategoryChildren($pid2);
            
            $sign = getSign($pid); //导航
        }else{
            //获取类目下面的小类目
            $zl = getCategoryChildren($id);
            
            $sign = getSign($id); //导航
        }
        
        $title = Db::name('Category')->find(isset($pid) ? $pid : $id)['name'];
        
        return $this->fetch('',['data'=>$data['data'],'page'=>$data['page'],'ranking'=>$ranking,'sort'=>isset($sort_arr) ? $sort_arr[0] : '','pl'=>$pl,'zl'=>$zl,'id'=> $id,'pid'=>$pid ? $pid : 0,'sign'=>$sign,'title'=>$title,'keywords'=>$title]);

    }
    
    
    /**
     * 搜索
     */
    public function search () {
        $order_by = 'add_time desc';
        $sort = input('param.sort');
        $key = input('param.keyword');
        
        $page = input('param.page');
        if (isset($page) && null !== $page) {
            $pages = $page;
        }
        else {
            $pages = 1;
        }
        
        $options=[
            'page'=>$pages,
            'path'=>'search?page=%&keyword='.$key,
        ];
        
        
        if($sort){
            $sort_arr = explode('-', $sort);
            if(in_array($sort_arr[1],array('asc','desc')) && in_array($sort_arr[0], array('add_time','click'))){
                $order_by = $sort_arr[0].' '.$sort_arr[1];
            }
        }
        
        $s = new \SphinxClient;
        $s->setServer(config('sphinx_id'), 9312);

        $s->setMatchMode(SPH_MATCH_PHRASE);
        $s->setMaxQueryTime(30);
        $s->setLimits (($pages-1)*36,36,1000,0);   
        $res = $s->query($key,'main'); 
        
        $err = $s->GetLastError();
        if($err)
            abort (404,$err);
            
        $result = array_keys(isset($res['matches']) ? $res['matches'] : []);
        
        $data =[];
        if($result){
           $data = Db::name('Movies')->where(['id'=>['in',  implode(',', $result)]])->order($order_by)->select();
           $data = \think\paginator\driver\Bootstrap::make($data,36,$pages,$res['total_found'],false,$options);
        }
        
        $page = $data ? $data->render() : '';
        
        return $this->fetch('search',['data'=>$data,'page'=>$page,'sort'=>isset($sort_arr) ? $sort_arr[0] : '','count'=>$data ? $data->total() : 0]);
    }
    
}
